var Extend = function(target,source){
	for(var prop in source){
		target[prop]=source[prop];
	}
}
var isEmptyObject = function(obj){
	for(var i in obj){
		return true;
	}
	return false;
}


var Plugins = function(){
	this.init.apply(this,arguments);
}
Plugins.prototype={
	init:function(options){
		this.setValue(options);
		this.content=this.options.content;
		this.target=this.options.target;
		this.speed=this.options.speed;
		this.DelSpeed=this.options.DelSpeed;
		this.verify();
	},
	setValue:function(options){
		this.options={
			asd:'234'
		}
		Extend(this.options,options||{});
	},
	timer:function(target,data){
		var that=this;
		var seq=0;
		var dtd = $.Deferred();
		function test(target,data){
			that.target.innerHTML+=data.substr(seq,1);
			if(seq==data.length){
				seq=0;
				that.target.innerHTML+='<br/>';
				dtd.resolve();
			}
			else{
				seq++;
				setTimeout(function(){test(target,data)},that.speed);
			}
		}
		test(target,data);
		return dtd.promise();
	},
	delay:function(time){
		var dtd=$.Deferred();
		var dlFunc=function(){
			dtd.resolve();
		}
		setTimeout(dlFunc,time);
		return dtd.promise();
	},
	clear:function(target,data){
		var that=this;
		var str=this.target.innerHTML;
		var seq=str.length;
		var dtd = $.Deferred();
		function delStr(target,data){
			that.target.innerHTML=str.substr(0,seq-1);
			if(that.target.innerHTML==0){
				dtd.resolve();
				return;
			}
			else{
				seq--;
				setTimeout(function(){delStr()},that.DelSpeed);
			}
		}
		delStr(target,data);
		return dtd.promise();
	},
	verify:function(){
		var that=this;
		var html='this.timer(this.target,this.content[0])';
		for(var i=1;i<this.content.length;i++){
			console.log(typeof this.content[i])
			if(this.content[i]=='clear'){
				html+='.then(function () {that.clear(that.target,that.content[' + i + '])';
			}
			else if(typeof this.content[i]=='number') {
				html+='.then(function () {that.delay(that.content[' + i + '])';
			}
			else{
				html+='.then(function () {that.timer(that.target,that.content[' + i + '])';
			}
		}
		for(var j=1;j<this.content.length;j++){
			html+='})';
		}
		eval(html);
		/*console.log(this.content[2])
		this.timer(this.target,this.content[0]).then(function () {
			that.timer(that.target,that.content[1]).then(function(){
				that.clear(that.target,that.content[2]).then(function(){
						that.timer(that.target,that.content[3])
					})
				})
			})*/
		
	}
}
